var express = require('express');
var router = express.Router();

router.all('/:id(\\d+)/?*', function(req, res, next) {
    var id = parseInt(req.params.id);
    if(!isNaN(id)) {
        req.model.a_id = id;
        req.pool.acquire(function(err, conn) {
            conn.lcall('articles.getTexts', {a_id: req.model.a_id}, function(err, result) {
                req.pool.release(conn);
                req.model.texts = result;
                next();
            });
        });
    } else {
        req.model.warningAlerts.push('wrong id');
        req.session.model = req.model;
        res.redirect("/Periodicals/");
    }
})

router.all('/:id(\\d+)/:t_id(\\d+)/?*', function(req, res, next) {
    var t_id = parseInt(req.params.t_id);
    if(!isNaN(t_id)) {
        req.model.t_id = t_id;
        next();
    } else {
        req.model.warningAlerts.push('wrong id');
        req.session.model = req.model;
        res.redirect("/Periodicals/");
    }
});

router.post('/:id(\\d+)/:t_id(\\d+)/update', function(req, res, next) {
    var params = {};
    params.t_id = parseInt(req.params.t_id);
    params.t_title = req.body.title;
    params.t_locale = req.body.locale;
    params.t_text = req.body.text;

    req.pool.acquire(function(err, conn) {
        conn.call('articles.updateText', params, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully updated');
            }
            req.session.model = req.model;
            res.redirect('/Articles/' + req.model.a_id);
        });
    });
});

router.post('/:id(\\d+)/create', function(req, res, next) {
    var params = {};
    params.a_id = req.model.a_id;
    params.t_title = req.body.title;
    params.t_locale = req.body.locale;
    params.t_text = req.body.text;

    req.pool.acquire(function(err, conn) {
        conn.call('articles.createText', params, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully created');
            }
            req.session.model = req.model;
            res.redirect('/Articles/' + req.model.a_id);
        });
    });
});

router.get('/:id(\\d+)/:t_id(\\d+)/delete', function(req, res, next) {
    req.pool.acquire(function(err, conn) {
        conn.call('articles.deleteText', {t_id: req.model.t_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Succesfully deleted');
            }
            req.session.model = req.model;
            res.redirect('/Articles/' + req.model.a_id);
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res, next) {
    res.render('articles', req.model);
});

module.exports = router;